package vg.services.log_manager.realization;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import vg.services.log_manager.interfaces.ILog;

/**
 * This class realizes interface ILog.
 * Messages saves in file log.txt. And saves old log file in directory log. 
 * @author tzolotuhin
 */
public class FileLog implements ILog {
	private DataOutputStream output;
	private String fileName;
	private final SimpleLog simpleLog;
	//-------------------------------------------------------------------------
	public FileLog() throws Exception {
		this.simpleLog = new SimpleLog();
		File dir = new File("log");
		if(!dir.exists() || (dir.isFile() && !dir.isFile())) {
			File f = new File("log");
			try {
				f.mkdir();
			} catch (Exception ex) {
				throw(ex);
			}
		}
		Date data = new Date();
		long time = data.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("dd.M.yyyy H.mm.ss");
		String timeS = sdf.format(time);
		FileOutputStream f = null;
		try {
			f = new FileOutputStream("log/"+timeS+"_log.txt");
			this.output = new DataOutputStream(f);
			this.fileName = dir.getAbsolutePath() + File.separatorChar + timeS + "_log.txt";
		} catch (IOException ex) {
			throw(new Exception("Warning : Couldn't to create file " + "log/"+timeS+"_log.txt"));
		}
	}
	//-------------------------------------------------------------------------
	public void printDebug(String message) {
		try {
			this.output.write(("[DEBUG] : " + message + "\n").getBytes());
			this.output.flush();
		} catch (IOException ex) {
			this.simpleLog.printDebug(message);
		}
	}
	public void printError(String message) {
		try {
			this.output.write(("[ERROR] : " + message + "\n").getBytes());
			this.output.flush();
		} catch (IOException ex) {
			this.simpleLog.printError(message);
		}
		
	}
	public void printInfo(String message) {
		try {
			this.output.write(("[INFO] : " + message + "\n").getBytes());
			this.output.flush();
		} catch (IOException ex) {
			this.simpleLog.printInfo(message);			
		}
	}
	public void printStackTrace(StackTraceElement[] trace) {
		if (trace == null)
			return;		
		try {
			this.output.write("[TRACE START]\n".getBytes());
			System.err.print("[TRACE START]\n");
			for (int i = 0; i < trace.length; i++) {
				this.output.write(("[TRACE] : " + trace[i].toString() + "\n").getBytes());
				System.err.print("[TRACE] : " + trace[i].toString() + "\n");
			}
			this.output.write("[TRACE END]\n".getBytes());
			System.err.print("[TRACE END]\n");
		} catch (IOException e) {
			this.simpleLog.printStackTrace(trace);
		}
	}
	public void printException(Throwable ex) {
		if (ex == null)
			return;		
		try {
			this.output.write(("[TRACE START] [" + ex.getMessage() + "]\n").getBytes());
			System.err.print(("[TRACE START] [" + ex.getMessage() + "]\n"));
			for (int i = 0; i < ex.getStackTrace().length; i++) {
				this.output.write(("[TRACE] : " + ex.getStackTrace()[i].toString() + "\n").getBytes());
				System.err.print("[TRACE] : " + ex.getStackTrace()[i].toString() + "\n");
			}
			this.output.write("[TRACE END]\n".getBytes());
			System.err.print("[TRACE END]\n");
		} catch (IOException e) {
			this.simpleLog.printException(ex);
		}		
	}
	public String getLoggerInfo() {
		String msg = "Log file : " + this.fileName;
		return(msg);
	}
}
